package com.example.demo.controller;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.demo.service.CaiwuService;
import com.example.demo.util.Utils;

@Controller
@CrossOrigin  //允许跨域请求
public class CaiwuController {

	// 依赖注入
	@Autowired
	private CaiwuService service;

	@Autowired
	private Utils utils;

	// 查询当年全部月份的销售、产量和存量数据信息
	@RequestMapping(value="/getPrdCount",method = RequestMethod.GET)
	@ResponseBody
	public JSON getPrdCount(HttpServletResponse response,HttpServletRequest request) throws Exception {
		//	this.response.setHeader("Access-Control-Allow-Origin", "http://localhost:8081/"); 
		String params = request.getParameter("year");
		System.out.println("参数:"+params);

		String year="";
		if(params==""|params==null|"".equals(params)) {
			year="2020";
		}
		else {
			year=params.substring(1,5);
		}
		//生成日期list
		List<String> datelist = new ArrayList(); 

		Map<String,Object> saleaccountmap = new HashMap();
		Map<String,Object> yieldmap = new HashMap();
		Map<String,Object> stockmap = new HashMap();
		Map<String,Object> datemap = new HashMap();


		List<Map<String,Object>> assetamountlist = new ArrayList(); 
		List<Map<String,Object>> fuzhailist = new ArrayList(); 
		List<Map<String,Object>> equitylist = new ArrayList(); 


		saleaccountmap.put("name","销量");
		yieldmap.put("name","产量");
		stockmap.put("name","存量");

		JSONObject json = new JSONObject();
		List<Map<String,Object>> list = service.getPrdCount(year);
		for(int i=0;i<list.size();i++) {
			Map<String,Object> salemap = new HashMap();
			Map<String,Object> yiemap = new HashMap();
			Map<String,Object> stocmap = new HashMap();
			salemap.put("number",list.get(i).get("saleaccount"));
			yiemap.put("number",list.get(i).get("yield"));
			stocmap.put("number",list.get(i).get("stock"));
			assetamountlist.add(salemap);
			fuzhailist.add(yiemap);
			equitylist.add(stocmap);
			//生成datelist
			datelist.add((String) list.get(i).get("date"));		
		}

		saleaccountmap.put("list",assetamountlist);
		yieldmap.put("list",fuzhailist);
		stockmap.put("list",equitylist);
		stockmap.put("datelist",datelist);
		//	datemap.put("datelist",datelist);


		//	JSONArray array= JSONArray.parseArray(JSON.toJSONString(list));
		JSONArray array = new JSONArray();
		array.add(saleaccountmap);
		array.add(yieldmap);
		array.add(stockmap);
		//	array.add(datemap);
		System.out.println(array.toJSONString());
		System.out.println(datelist.toString());
		return array;
	}

	//查询当年的销售、产量和存量数据  echarts专用
	@RequestMapping(value="/getPrdCountForecharts",method = RequestMethod.GET)
	@ResponseBody
	public JSON getPrdCountForecharts(HttpServletResponse response,HttpServletRequest request) throws Exception {
		String params = request.getParameter("year");
		System.out.println("参数:"+params);

		String year="";
		if(params==""|params==null|"".equals(params)) {
			year="2020";
		}
		else {
			year=params.substring(1,5);
		}
		System.out.println("year:"+year);
		//生成日期list
		List<String> datelist = new ArrayList(); 


		List<Object> incomelist = new ArrayList(); 
		List<Object> fuzhailist = new ArrayList(); 
		List<Object> equitylist = new ArrayList(); 

		List<Map<String,Object>> list = service.getPrdCount(year);
		for(int i=0;i<list.size();i++) {
			incomelist.add(list.get(i).get("saleaccount"));
			fuzhailist.add(list.get(i).get("yield"));
			equitylist.add(list.get(i).get("stock"));
			//生成datelist
			datelist.add((String) list.get(i).get("date"));		
		}

		JSONArray array = new JSONArray();
		array.add(datelist);
		array.add(incomelist);
		array.add(fuzhailist);
		array.add(equitylist);
		System.out.println(array.toJSONString());
		return array;
	}

	//查询当月-1月的销量、产量、存量的环比、同比数据
	@RequestMapping(value="/getPrdCountPercent",method = RequestMethod.GET)
	@ResponseBody
	public JSON getPrdCountPercent(HttpServletResponse response,HttpServletRequest request) throws Exception {

		Map<String,Object> totalMap = new HashMap<String, Object>();
		
		String params = request.getParameter("year");
		String lastmonth="";
		String cuerrentmonth="";
		System.out.println("参数:"+params);
		String lastyearmonth="";

		String year="";
		if(params==""|params==null|"".equals(params)) {
			year="2020";
			//当前年月-1月
			 lastmonth = Utils.getTimeDay("yyyyMM", -1);
			 //当月
			 cuerrentmonth = Utils.getTimeDay("yyyyMM",0);
			//当前年-1年
			 lastyearmonth = Utils.getTimeDay("yyyyMM", -12);
		}
		else {
			year=params.substring(1,5);
				//当前年月-1月
			lastmonth = Utils.dealTime(year,params.substring(6,8),-2);
				//当月
		     cuerrentmonth = year+params.substring(6,8);
				//上年当月 当前年-1年
		     lastyearmonth = Utils.dealTime(year,params.substring(6,8), -13);
		}

		System.out.println("当月"+cuerrentmonth);
		System.out.println("上月"+lastmonth);
		System.out.println("上一年当月"+lastyearmonth);

		//当前年
//		String currentyear = cuerrentmonth.substring(0,4); 

		//	当年销量、产量、存量
		Map<String,Object> prdcountmap = new HashMap<String, Object>();
		prdcountmap = service.getprdcountyear(year);

		//当月销量、产量、存量
		Map<String,Object> assetmap = new HashMap<String, Object>();
		assetmap = service.getprdcountmonth(cuerrentmonth);

		//上个月销量、产量、存量
		Map<String,Object> lastassetmap = new HashMap<String, Object>();
		lastassetmap = service.getprdcountmonth(lastmonth);

		//  上年同月销量、产量、存量
		Map<String,Object> firstassetmap = new HashMap<String, Object>();
		firstassetmap = service.getprdcountmonth(lastyearmonth);

		// 销量环比
		Double prdcounthb = utils.mathtwo(((Double) assetmap.get("saleaccount")-(Double)lastassetmap.get("saleaccount"))/(Double)lastassetmap.get("saleaccount")*100);
		// 产量环比
		Double yieldhb = utils.mathtwo(((Double) assetmap.get("yield")-(Double)lastassetmap.get("yield"))/(Double)lastassetmap.get("yield")*100);
		// 存量同环比
		Double stockhb =  utils.mathtwo(((Double) assetmap.get("stock")-(Double)lastassetmap.get("stock"))/(Double)lastassetmap.get("stock")*100);

		// 销量同比
		Double prdcounttb = utils.mathtwo(((Double) assetmap.get("saleaccount")-(Double)firstassetmap.get("saleaccount"))/(Double)firstassetmap.get("saleaccount")*100);
		// 产量同比
		Double yieldtb = utils.mathtwo(((Double) assetmap.get("yield")-(Double)firstassetmap.get("yield"))/(Double)firstassetmap.get("yield")*100);
		// 存量同比
		Double stocktb =  utils.mathtwo(((Double) assetmap.get("stock")-(Double)firstassetmap.get("stock"))/(Double)firstassetmap.get("stock")*100);

		totalMap.put("prdcount",prdcountmap.get("saleaccounttotal"));
		totalMap.put("yield",prdcountmap.get("yieldtotal"));
		totalMap.put("stock",prdcountmap.get("stocktotoal"));

		totalMap.put("prdcounthb",prdcounthb);
		totalMap.put("yieldhb",yieldhb);
		totalMap.put("stockhb",stockhb);

		totalMap.put("prdcounttb",prdcounttb);
		totalMap.put("yieldtb",yieldtb);
		totalMap.put("stocktb",stocktb);

		JSONArray array = new JSONArray();
		array.add(totalMap);
		System.out.println(array.toJSONString());
		return array;
	}

	// 查询当年的资产、负债和所有者权益数据
	@RequestMapping(value="/getAsset",method = RequestMethod.GET)
	@ResponseBody
	public JSON getAsset(HttpServletResponse response,HttpServletRequest request) throws Exception {
		//	this.response.setHeader("Access-Control-Allow-Origin", "http://localhost:8081/"); 
		String params = request.getParameter("year");
		System.out.println("参数:"+params);

		String year="";
		if(params==""|params==null|"".equals(params)) {
			year="2020";
		}
		else {
			year=params.substring(1,5);
		}

		//生成日期list
		List<String> datelist = new ArrayList(); 

		Map<String,Object> saleaccountmap = new HashMap();
		Map<String,Object> yieldmap = new HashMap();
		Map<String,Object> stockmap = new HashMap();
		Map<String,Object> datemap = new HashMap();


		List<Map<String,Object>> incomelist = new ArrayList(); 
		List<Map<String,Object>> fuzhailist = new ArrayList(); 
		List<Map<String,Object>> equitylist = new ArrayList(); 


		saleaccountmap.put("name","资产");
		yieldmap.put("name","负债");
		stockmap.put("name","所有者权益");

		JSONObject json = new JSONObject();
		List<Map<String,Object>> list = service.getAsset(year);
		for(int i=0;i<list.size();i++) {
			Map<String,Object> salemap = new HashMap();
			Map<String,Object> yiemap = new HashMap();
			Map<String,Object> stocmap = new HashMap();
			salemap.put("number",utils.mathtwo(Double.valueOf((String) list.get(i).get("assetamount"))/10000));
			yiemap.put("number",utils.mathtwo(Double.valueOf((String) list.get(i).get("fuzhai"))/10000));
			stocmap.put("number",utils.mathtwo(Double.valueOf((String) list.get(i).get("equity"))/10000));
			incomelist.add(salemap);
			fuzhailist.add(yiemap);
			equitylist.add(stocmap);
			//生成datelist
			datelist.add((String) list.get(i).get("date"));		
		}

		saleaccountmap.put("list",incomelist);
		yieldmap.put("list",fuzhailist);
		stockmap.put("list",equitylist);
		stockmap.put("datelist",datelist);


		//	JSONArray array= JSONArray.parseArray(JSON.toJSONString(list));
		JSONArray array = new JSONArray();
		array.add(saleaccountmap);
		array.add(yieldmap);
		array.add(stockmap);
		//	array.add(datemap);
		System.out.println(array.toJSONString());
		return array;
	}


	//查询某年12月的资产、负债和所有者权益数据  echarts专用
	@RequestMapping(value="/getAssetForecharts",method = RequestMethod.GET)
	@ResponseBody
	public JSON getAssetForecharts(HttpServletResponse response,HttpServletRequest request) throws Exception {
		String params = request.getParameter("year");
		System.out.println("参数:"+params);

		String year="";
		if(params==""|params==null|"".equals(params)) {
			year="2020";
		}
		else {
			year=params.substring(1,5);
		}
		System.out.println("year:"+year);
		//生成日期list
		List<String> datelist = new ArrayList(); 


		List<Object> incomelist = new ArrayList(); 
		List<Object> fuzhailist = new ArrayList(); 
		List<Object> equitylist = new ArrayList(); 

		List<Map<String,Object>> list = service.getAsset(year);
		for(int i=0;i<list.size();i++) {
			incomelist.add(utils.mathtwo(Double.valueOf((String) list.get(i).get("assetamount"))/10000));
			fuzhailist.add(utils.mathtwo(Double.valueOf((String) list.get(i).get("fuzhai"))/10000));
			equitylist.add(utils.mathtwo(Double.valueOf((String) list.get(i).get("equity"))/10000));
			//生成datelist
			datelist.add((String) list.get(i).get("date"));		
		}

		JSONArray array = new JSONArray();
		array.add(datelist);
		array.add(incomelist);
		array.add(fuzhailist);
		array.add(equitylist);
		System.out.println(array.toJSONString());
		return array;
	}


	//查询资产、负债和所有者权益环比和比年初数据
	@RequestMapping(value="/getAssetPercent",method = RequestMethod.GET)
	@ResponseBody
	public JSON getAssetPercent(HttpServletResponse response,HttpServletRequest request) throws Exception {

		Map<String,Object> totalMap = new HashMap<String, Object>();
		//当年第一月
		String startmonth ="";
		
		String params = request.getParameter("year");
		String lastmonth="";
		String cuerrentmonth="";
		System.out.println("参数:"+params);
		String lastyearmonth="";

		String year="";
		if(params==""|params==null|"".equals(params)) {
			year="2020";
			//当前年月-1月
			 lastmonth = Utils.getTimeDay("yyyyMM", -1);
			 //当月
			 cuerrentmonth = Utils.getTimeDay("yyyyMM",0);
			//当前年-1年
			 lastyearmonth = Utils.getTimeDay("yyyyMM", -12);
			 startmonth = year+"01";
		}
		else {
			year=params.substring(1,5);
				//当前年月-1月
			lastmonth = Utils.dealTime(year,params.substring(6,8),-2);
				//当月
		     cuerrentmonth = year+params.substring(6,8);
				//上年当月 当前年-1年
		     lastyearmonth = Utils.dealTime(year,params.substring(6,8), -13);
		     startmonth = year+"01";
		}

		System.out.println("当月"+cuerrentmonth);
		System.out.println("上月"+lastmonth);
		System.out.println("当前年"+year);
		System.out.println("当年第一个月"+startmonth);



		//	当月资产总额、负债总额、所有者权益总额
		Map<String,Object> assetmap = new HashMap<String, Object>();
		assetmap = service.getAssetMonth(cuerrentmonth);

		//上月资产总额、负债总额、所有者权益总额
		Map<String,Object> lastassetmap = new HashMap<String, Object>();
		lastassetmap = service.getAssetMonth(lastmonth);

		//年初资产总额、负债总额、所有者权益总额
		Map<String,Object> firstassetmap = new HashMap<String, Object>();
		firstassetmap = service.getAssetMonth(startmonth);


		// 资产总额环比
		Double assetamounthb = utils.mathtwo(((Double) assetmap.get("assetamount")-(Double)lastassetmap.get("assetamount"))/(Double)lastassetmap.get("assetamount")*100);
		// 负债总额环比
		Double fuzhaihb = utils.mathtwo(((Double) assetmap.get("fuzhai")-(Double)lastassetmap.get("fuzhai"))/(Double)lastassetmap.get("fuzhai")*100);
		// 所有者权益环比
		Double equityhb =  utils.mathtwo(((Double) assetmap.get("equity")-(Double)lastassetmap.get("equity"))/(Double)lastassetmap.get("equity")*100);

		// 资产总额比年初
		Double assetamountnc = utils.mathtwo(((Double) assetmap.get("assetamount")-(Double)firstassetmap.get("assetamount"))/(Double)firstassetmap.get("assetamount")*100);
		// 负债总额比年初
		Double fuzhainc = utils.mathtwo(((Double) assetmap.get("fuzhai")-(Double)firstassetmap.get("fuzhai"))/(Double)firstassetmap.get("fuzhai")*100);
		// 所有者权益比年初
		Double equitync =  utils.mathtwo(((Double) assetmap.get("equity")-(Double)firstassetmap.get("equity"))/(Double)firstassetmap.get("equity")*100);

		totalMap.put("assetamount",utils.mathtwo((Double)assetmap.get("assetamount")/10000));
		totalMap.put("fuzhai",utils.mathtwo((Double)assetmap.get("fuzhai")/10000));
		totalMap.put("equity",utils.mathtwo((Double)assetmap.get("equity")/10000));

		totalMap.put("assetamounthb",assetamounthb);
		totalMap.put("fuzhaihb",fuzhaihb);
		totalMap.put("equityhb",equityhb);

		totalMap.put("assetamountnc",assetamountnc);
		totalMap.put("fuzhainc",fuzhainc);
		totalMap.put("equitync",equitync);

		JSONArray array = new JSONArray();
		array.add(totalMap);
		System.out.println(array.toJSONString());
		return array;
	}


	// 查询当年利润、营业收入数据for 下方表格
	@RequestMapping(value="/getEarn",method = RequestMethod.GET)
	@ResponseBody
	public JSON getEarn(HttpServletResponse response,HttpServletRequest request) throws Exception {
		//	this.response.setHeader("Access-Control-Allow-Origin", "http://localhost:8081/"); 
		String params = request.getParameter("year");
		System.out.println("参数:"+params);

		String year="";
		if(params==""|params==null|"".equals(params)) {
			year="2020";
		}
		else {
			year=params.substring(1,5);
		}

		//生成日期list
		List<String> datelist = new ArrayList(); 

		Map<String,Object> incomemap = new HashMap();
		Map<String,Object> profitsmap = new HashMap();
		//					Map<String,Object> stockmap = new HashMap();
		Map<String,Object> datemap = new HashMap();


		List<Map<String,Object>> incomelist = new ArrayList(); 
		List<Map<String,Object>> profitslist = new ArrayList(); 
		//					List<Map<String,Object>> equitylist = new ArrayList(); 


		incomemap.put("name","营业收入");
		profitsmap.put("name","利润");
		//					stockmap.put("name","所有者权益");

		JSONObject json = new JSONObject();
		List<Map<String,Object>> list = service.getEarn(year);
		for(int i=0;i<list.size();i++) {
			Map<String,Object> income = new HashMap();
			Map<String,Object> profits = new HashMap();
			//						Map<String,Object> stocmap = new HashMap();
			income.put("number",utils.mathtwo(Double.valueOf((String) list.get(i).get("income"))/10000));
			profits.put("number",utils.mathtwo(Double.valueOf((String) list.get(i).get("profits"))/10000));
			//						stocmap.put("number",utils.mathtwo(Double.valueOf((String) list.get(i).get("equity"))/10000));
			incomelist.add(income);
			profitslist.add(profits);
			//						equitylist.add(stocmap);
			//生成datelist
			datelist.add((String) list.get(i).get("date"));		
		}

		incomemap.put("list",incomelist);
		profitsmap.put("list",profitslist);
		profitsmap.put("datelist",datelist);

		JSONArray array = new JSONArray();
		array.add(incomemap);
		array.add(profitsmap);
		System.out.println(array.toJSONString());
		return array;
	}

	//查询某年12个月的利润总额、营业收入  echarts专用
	@RequestMapping(value="/getEarnForecharts",method = RequestMethod.GET)
	@ResponseBody
	public JSON getEarnForecharts(HttpServletResponse response,HttpServletRequest request) throws Exception {
		String params = request.getParameter("year");
		System.out.println("参数:"+params);

		String year="";
		if(params==""|params==null|"".equals(params)) {
			year="2020";
		}
		else {
			year=params.substring(1,5);
		}
		System.out.println("year:"+year);
		//生成日期list
		List<String> datelist = new ArrayList(); 


		List<Object> incomelist = new ArrayList(); 
		List<Object> profitslist = new ArrayList(); 

		List<Map<String,Object>> list = service.getEarn(year);
		for(int i=0;i<list.size();i++) {
			incomelist.add(utils.mathtwo(Double.valueOf((String) list.get(i).get("income"))/10000));
			profitslist.add(utils.mathtwo(Double.valueOf((String) list.get(i).get("profits"))/10000));
			//生成datelist
			datelist.add((String) list.get(i).get("date"));		
		}

		JSONArray array = new JSONArray();
		array.add(datelist);
		array.add(incomelist);
		array.add(profitslist);
		System.out.println(array.toJSONString());
		return array;
	}

	//查询营业收入和利润环比和同比数据
	@RequestMapping(value="/getEarnPercent",method = RequestMethod.GET)
	@ResponseBody
	public JSON getEarnPercent(HttpServletResponse response,HttpServletRequest request) throws Exception {

		Map<String,Object> totalMap = new HashMap<String, Object>();

		String params = request.getParameter("year");
		String lastmonth="";
		String cuerrentmonth="";
		System.out.println("参数:"+params);
		String lastyearmonth="";

		String year="";
		if(params==""|params==null|"".equals(params)) {
			year="2020";
			//当前年月-1月
			 lastmonth = Utils.getTimeDay("yyyyMM", -1);
			 //当月
			 cuerrentmonth = Utils.getTimeDay("yyyyMM",0);
			//当前年-1年
			 lastyearmonth = Utils.getTimeDay("yyyyMM", -12);
		}
		else {
			year=params.substring(1,5);
				//当前年月-1月
			lastmonth = Utils.dealTime(year,params.substring(6,8),-2);
				//当月
		     cuerrentmonth = year+params.substring(6,8);
				//上年当月 当前年-1年
		     lastyearmonth = Utils.dealTime(year,params.substring(6,8), -13);
		}

		System.out.println("当月"+cuerrentmonth);
		System.out.println("上月"+lastmonth);
		System.out.println("上一年当月"+lastyearmonth);

		//当前年
		String currentyear = lastmonth.substring(0,4); 

		//	当年营收、利润总额
		Map<String,Object> erantotalmap = new HashMap<String, Object>();
		erantotalmap = service.getearnyear(currentyear);

		//当月营业收入和利润
		Map<String,Object> earnmap = new HashMap<String, Object>();
		earnmap = service.getearnmonth(cuerrentmonth);

		//上个月营业收入和利润
		Map<String,Object> lastassetmap = new HashMap<String, Object>();
		lastassetmap = service.getearnmonth(lastmonth);

		//  上年同月营业收入和利润
		Map<String,Object> firstassetmap = new HashMap<String, Object>();
		firstassetmap = service.getearnmonth(lastyearmonth);

		// 营业收入环比
		Double incomehb = utils.mathtwo(((Double) earnmap.get("income")-(Double)lastassetmap.get("income"))/(Double)lastassetmap.get("income")*100);
		// 利润环比
		Double profitshb = utils.mathtwo(((Double) earnmap.get("profits")-(Double)lastassetmap.get("profits"))/(Double)lastassetmap.get("profits")*100);

		// 营业收入同比
		Double incometb = utils.mathtwo(((Double) earnmap.get("income")-(Double)firstassetmap.get("income"))/(Double)firstassetmap.get("income")*100);
		// 利润同比
		Double profitstb = utils.mathtwo(((Double) earnmap.get("profits")-(Double)firstassetmap.get("profits"))/(Double)firstassetmap.get("profits")*100);

		totalMap.put("income",utils.mathtwo((Double)erantotalmap.get("income")/10000));
		totalMap.put("profits",utils.mathtwo((Double)erantotalmap.get("profits")/10000));

		totalMap.put("incomehb",incomehb);
		totalMap.put("incometb",incometb);

		totalMap.put("profitshb",profitshb);
		totalMap.put("profitstb",profitstb);

		JSONArray array = new JSONArray();
		array.add(totalMap);
		System.out.println(array.toJSONString());
		return array;
	}

	// 查询当年成本数据for 下方表格
	@RequestMapping(value="/getChengben",method = RequestMethod.GET)
	@ResponseBody
	public JSON getChengben(HttpServletResponse response,HttpServletRequest request) throws Exception {
		//	this.response.setHeader("Access-Control-Allow-Origin", "http://localhost:8081/"); 
		String params = request.getParameter("year");
		System.out.println("参数:"+params);

		String year="";
		if(params==""|params==null|"".equals(params)) {
			year="2020";
		}
		else {
			year=params.substring(1,5);
		}

		//生成日期list
		List<String> datelist = new ArrayList(); 

		Map<String,Object> chengbenmap = new HashMap();
		//			Map<String,Object> profitsmap = new HashMap();
		//			Map<String,Object> stockmap = new HashMap();
		Map<String,Object> datemap = new HashMap();


		List<Map<String,Object>> chengbenlist = new ArrayList(); 

		chengbenmap.put("name","营业成本");

		JSONObject json = new JSONObject();
		List<Map<String,Object>> list = service.getEarn(year);
		for(int i=0;i<list.size();i++) {
			Map<String,Object> chengben = new HashMap();

			chengben.put("number",utils.mathtwo(Double.valueOf((String) list.get(i).get("chengben"))/10000));

			chengbenlist.add(chengben);

			//生成datelist
			datelist.add((String) list.get(i).get("date"));		
		}

		chengbenmap.put("list",chengbenlist);
		chengbenmap.put("datelist",datelist);

		JSONArray array = new JSONArray();
		array.add(chengbenmap);
		System.out.println(array.toJSONString());
		return array;
	}


	//查询某年12个月的成本数据 for echarts
	@RequestMapping(value="/getChengbenForecharts",method = RequestMethod.GET)
	@ResponseBody
	public JSON getChengbenForecharts(HttpServletResponse response,HttpServletRequest request) throws Exception {
		String params = request.getParameter("year");
		System.out.println("参数:"+params);

		String year="";
		if(params==""|params==null|"".equals(params)) {
			year="2020";
		}
		else {
			year=params.substring(1,5);
		}
		System.out.println("year:"+year);
		//生成日期list
		List<String> datelist = new ArrayList(); 


		List<Object> chengbenlist = new ArrayList(); 
		//	List<Object> profitslist = new ArrayList(); 

		List<Map<String,Object>> list = service.getEarn(year);
		for(int i=0;i<list.size();i++) {
			chengbenlist.add(utils.mathtwo(Double.valueOf((String) list.get(i).get("chengben"))/10000));
			//		profitslist.add(utils.mathtwo(Double.valueOf((String) list.get(i).get("profits"))/10000));
			//生成datelist
			datelist.add((String) list.get(i).get("date"));		
		}

		JSONArray array = new JSONArray();
		array.add(datelist);
		array.add(chengbenlist);
		//	array.add(profitslist);
		System.out.println(array.toJSONString());
		return array;
	}

	//查询成本环比和同比数据
	@RequestMapping(value="/getChengbenPercent",method = RequestMethod.GET)
	@ResponseBody
	public JSON getChengbenPercent(HttpServletResponse response,HttpServletRequest request) throws Exception {

		Map<String,Object> totalMap = new HashMap<String, Object>();

		String params = request.getParameter("year");
		String lastmonth="";
		String cuerrentmonth="";
		System.out.println("参数:"+params);
		String lastyearmonth="";

		String year="";
		if(params==""|params==null|"".equals(params)) {
			year="2020";
			//当前年月-1月
			 lastmonth = Utils.getTimeDay("yyyyMM", -1);
			 //当月
			 cuerrentmonth = Utils.getTimeDay("yyyyMM",0);
			//当前年-1年
			 lastyearmonth = Utils.getTimeDay("yyyyMM", -12);
		}
		else {
			year=params.substring(1,5);
				//当前年月-1月
			lastmonth = Utils.dealTime(year,params.substring(6,8),-2);
				//当月
		     cuerrentmonth = year+params.substring(6,8);
				//上年当月 当前年-1年
		     lastyearmonth = Utils.dealTime(year,params.substring(6,8), -13);
		}

		System.out.println("当月"+cuerrentmonth);
		System.out.println("上月"+lastmonth);
		System.out.println("上一年当月"+lastyearmonth);

		//当前年
		String currentyear = lastmonth.substring(0,4); 

		//	当年营业成本总额
		Map<String,Object> chengbentotalmap = new HashMap<String, Object>();
		chengbentotalmap = service.getearnyear(currentyear);

		//当月成本
		Map<String,Object> earnmap = new HashMap<String, Object>();
		earnmap = service.getearnmonth(cuerrentmonth);

		//上个月成本
		Map<String,Object> lastassetmap = new HashMap<String, Object>();
		lastassetmap = service.getearnmonth(lastmonth);

		//  上年同月成本
		Map<String,Object> firstassetmap = new HashMap<String, Object>();
		firstassetmap = service.getearnmonth(lastyearmonth);

		// 成本环比
		Double chengbenhb = utils.mathtwo(((Double) earnmap.get("chengben")-(Double)lastassetmap.get("chengben"))/(Double)lastassetmap.get("chengben")*100);
		// 成本同比
		Double chengbentb = utils.mathtwo(((Double) earnmap.get("chengben")-(Double)firstassetmap.get("chengben"))/(Double)firstassetmap.get("chengben")*100);

		totalMap.put("chengben",utils.mathtwo((Double)chengbentotalmap.get("chengben")/10000));

		totalMap.put("chengbenhb",chengbenhb);
		totalMap.put("chengbentb",chengbentb);


		JSONArray array = new JSONArray();
		array.add(totalMap);
		System.out.println(array.toJSONString());
		return array;
	}
	
	//查询总体盈利情况数据
		@RequestMapping(value="/getWholePercent",method = RequestMethod.GET)
		@ResponseBody
		public JSON getWholePercent(HttpServletResponse response,HttpServletRequest request) throws Exception {
			Map<String,Object> totalMap = new HashMap<String, Object>();
	        String params = request.getParameter("year");
			System.out.println("前台传过来的参数"+params);
	        String lastmonth="";
	        String cuerrentmonth="";
	        String lastyearmonth="";
	        String startmonth="";
	        String currentyear="";
	        String lastyear="";
	        if(null==params|"".equals(params)|""==params) {
//				当前年月-1月
			    lastmonth = Utils.getTimeDay("yyyyMM", -1);
				//当月
				 cuerrentmonth = Utils.getTimeDay("yyyyMM",0);
				//当前年-1年
				 lastyearmonth = Utils.getTimeDay("yyyyMM", -12);

				//当年年初
				 startmonth = cuerrentmonth.substring(0,4)+"01";
				//当前年
				 currentyear = lastmonth.substring(0,4); 
				 //去年
				 lastyear = lastyearmonth.substring(0,4); 
	        }
	        else {
	        	//当前年
				 currentyear = params.substring(1,5);
				//当前年月-1月
			    lastmonth = Utils.dealTime(currentyear,params.substring(6,8),-2);
				//当月
				 cuerrentmonth = currentyear+params.substring(6,8);
				//上年当月 当前年-1年
				 lastyearmonth = Utils.dealTime(currentyear,params.substring(6,8), -13);

				//当年年初
				 startmonth = cuerrentmonth.substring(0,4)+"01";
 
				 //去年
				 lastyear = lastyearmonth.substring(0,4); 
	        }

			System.out.println("当前年"+currentyear);
			System.out.println("去年"+lastyear);
			System.out.println("年初"+startmonth);
			System.out.println("当月"+cuerrentmonth);
			System.out.println("上月"+lastmonth);
			System.out.println("上一年当月"+lastyearmonth);


			//本年营收、利润总额
			Map<String,Object> wholemap = new HashMap<String, Object>();
			wholemap = service.getwholeearnyear();
			//	当年营收、利润总额
			Map<String,Object> erantotalmap = new HashMap<String, Object>();
			erantotalmap = service.getearnyear(currentyear);

			//上年营收、利润总额
			Map<String,Object> lasterantotalmap = new HashMap<String, Object>();
			lasterantotalmap = service.getearnyear(lastyear);
			//当月营业收入和利润
			Map<String,Object> earnmap = new HashMap<String, Object>();
			earnmap = service.getearnmonth(cuerrentmonth);

			//上个月营业收入和利润
			Map<String,Object> lastassetmap = new HashMap<String, Object>();
			lastassetmap = service.getearnmonth(lastmonth);

			//上年同月营业收入和利润
			Map<String,Object> firstassetmap = new HashMap<String, Object>();
			firstassetmap = service.getearnmonth(lastyearmonth);
			//  年初利润
			Map<String,Object> startassetmap = new HashMap<String, Object>();
			startassetmap = service.getearnmonth(startmonth);



			// 营业收入环比
			Double incomehb = utils.mathtwo(((Double) earnmap.get("income")-(Double)lastassetmap.get("income"))/(Double)lastassetmap.get("income")*100);
			// 利润环比
			Double profitshb = utils.mathtwo((((Double) earnmap.get("profits")-(Double)lastassetmap.get("profits"))/10000));
			//本月利润率
			Double profitsrate = utils.mathtwo((Double)earnmap.get("profits")/(Double)earnmap.get("income")*100);
			//本年累计利润率
			Double wholeprofitsrate = utils.mathtwo((Double)erantotalmap.get("profits")/(Double)erantotalmap.get("income")*100);
			//上年利润率
			Double lastwholeprofitsrate = utils.mathtwo((Double)lasterantotalmap.get("profits")/(Double)lasterantotalmap.get("income")*100);
			
			//上月利润率
			Double lastprofitsrate = utils.mathtwo((Double)lastassetmap.get("profits")/(Double)lastassetmap.get("income")*100);
			
			//上年当月利润率
			Double lastyearprofitsrate = utils.mathtwo((Double)firstassetmap.get("profits")/(Double)firstassetmap.get("income")*100);
			
			//当月利润同比增减量
			Double profitsratetb = utils.mathtwo(profitsrate-lastyearprofitsrate);

			//年初利润率
			Double startprofitsrate = utils.mathtwo((Double)startassetmap.get("profits")/(Double)lastassetmap.get("income")*100);

			// 本月利润率环比
			Double profitsratehb = utils.mathtwo(profitsrate-lastprofitsrate);

			// 利润率环比年初
			Double profitsratenc = utils.mathtwo(profitsrate-startprofitsrate);

			// 本年累计利润率同比量
			Double wholeprofitsratetb = utils.mathtwo(wholeprofitsrate-lastwholeprofitsrate);

			// 本年累计利润率同比率增减
			Double wholeprofitsratetbl =  utils.mathtwo((((Double) erantotalmap.get("profits")-(Double)lasterantotalmap.get("profits"))/(Double)lasterantotalmap.get("profits"))*100);


			// 当月利润同比增减
			Double profitstb = utils.mathtwo((((Double) earnmap.get("profits")-(Double)firstassetmap.get("profits"))/10000));

			// 累计利润比上年增减额
			Double wholeprofitstb = utils.mathtwo((((Double) erantotalmap.get("profits")-(Double)lasterantotalmap.get("profits"))/10000));




			//本月利润
			totalMap.put("profits",utils.mathtwo((Double)earnmap.get("profits")/10000));
			//上月利润数额
			totalMap.put("lastprofits",utils.mathtwo((Double)lastassetmap.get("profits")/10000));
			//去年本月利润数额
			totalMap.put("lasyeartprofits",utils.mathtwo((Double)firstassetmap.get("profits")/10000));

			//本年累计利润
			totalMap.put("wholeprofits",utils.mathtwo((Double)erantotalmap.get("profits")/10000));
			//去年累计利润
			totalMap.put("lastwholeprofits",utils.mathtwo((Double)lasterantotalmap.get("profits")/10000));

			//本月利润率
			totalMap.put("profitsrate",profitsrate);
			
			//上月利润率
			totalMap.put("lastprofitsrate",lastprofitsrate);
			
			//去年本月利润率
			totalMap.put("lastyearprofitsrate",lastyearprofitsrate);

			//本年累计利润率
			totalMap.put("wholeprofitsrate",wholeprofitsrate);
			//去年累计利润率
			totalMap.put("lastwholeprofitsrate",lastwholeprofitsrate);
			//本月利润率环比
			totalMap.put("profitsratehb",profitsratehb);
			//本月利润率同比增减量
			totalMap.put("profitsratetb",profitsratetb);
			//本月利润率比年初
			totalMap.put("profitsratenc",profitsratenc);
			//累计利润率同比
			totalMap.put("wholeprofitsratetb",wholeprofitsratetb);

			//本月利润环比
			totalMap.put("profitshb",profitshb);
			//累计利润比上年增减数额
			totalMap.put("wholeprofitstb",wholeprofitstb);
			//累计利润比上年增减率
			totalMap.put("wholeprofitsratetbl",wholeprofitsratetbl);
			//本月利润同比
			totalMap.put("profitstb",profitstb);



			JSONArray array = new JSONArray();
			array.add(totalMap);
			System.out.println(array.toJSONString());
			return array;
		}
}
